package com.foreknow.service.impl;

import com.foreknow.entity.Rule;
import com.foreknow.entity.Scheduling;
import com.foreknow.entity.User;
import com.foreknow.mapper.SchedulingMapper;
import com.foreknow.mapper.UserMapper;
import com.foreknow.service.SchedulingService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
public class SchedulingServiceImpl implements SchedulingService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SchedulingMapper schedulingMapper;

    /**
     * 查询排班计划(为了方便数据的使用，单独一条数据一条数据去查)
     * @param deptCategoryId
     * @param deptId
     * @param registLeId
     * @param start_date
     * @param end_date
     * @return
     */
    @Override
    public List<Object> getScheduling(Integer deptCategoryId, Integer deptId, Integer registLeId,String start_date, String end_date) {
        LocalDate localdate = LocalDate.now();
        LocalDate startDate = null;
        LocalDate endDate = null;
        if (start_date == null||start_date.equals("")){
            startDate = localdate.minusDays(3);
        } else {
            startDate = LocalDate.parse(start_date,DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }
        if (end_date == null||end_date.equals("")){
            endDate = localdate.plusDays(3);
        }else {
            endDate = LocalDate.parse(end_date,DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }
        LocalDate date = startDate;
        List<Object> list = new ArrayList<>();
        List<User> userList = userMapper.getUserList(deptCategoryId,deptId,registLeId);
        for (User user:userList) {
            //先普查一遍，筛选掉没有任何排班计划的
            List<Scheduling> result0 = schedulingMapper.getSchedulings(user.getId(),startDate,endDate);
            if (result0.size() !=0 ){
                List<Scheduling> schedulingList = new ArrayList<>();
                //循环日期
                while(date.compareTo(endDate)<=0){
                    //上午
                    Scheduling result1 = schedulingMapper.getScheduling(user.getId(),"上午",date);
                    if (result1 != null){
                        result1.setRealName(user.getRealName());
                        result1.setOnDuty(true);
                        schedulingList.add(result1);
                    }else{
                        Scheduling scheduling = new Scheduling();
                        scheduling.setRealName(user.getRealName());
                        scheduling.setUserId(user.getId());
                        scheduling.setDeptId(user.getDeptId());
                        scheduling.setRuleId(0);
                        scheduling.setSchedDate(date);
                        scheduling.setNoon("上午");
                        scheduling.setOnDuty(false);
                        schedulingList.add(scheduling);
                    }
                    //下午
                    Scheduling result2 = schedulingMapper.getScheduling(user.getId(),"下午",date);
                    if (result2 != null){
                        result2.setRealName(user.getRealName());
                        result2.setOnDuty(true);
                        schedulingList.add(result2);
                    }else{
                        Scheduling scheduling = new Scheduling();
                        scheduling.setRealName(user.getRealName());
                        scheduling.setUserId(user.getId());
                        scheduling.setDeptId(user.getDeptId());
                        scheduling.setRuleId(0);
                        scheduling.setSchedDate(date);
                        scheduling.setNoon("下午");
                        scheduling.setOnDuty(false);
                        schedulingList.add(scheduling);
                    }
                    date = date.plusDays(1);
                }
                list.add(schedulingList);
                date = startDate;
            }
        }
        return list;
    }

    /**
     * 添加排班计划
     * @param list
     * @param startDate
     * @param endDate
     */
    @Override
    @Transactional
    public Boolean addScheduling(List<Rule> list, LocalDate startDate, LocalDate endDate) {
        LocalDate date = LocalDate.now();
        date = date.compareTo(startDate)>=0?date:startDate;
        Scheduling scheduling = new Scheduling();
        while(date.compareTo(endDate)<=0){
            scheduling.setSchedDate(date);
            Integer week = date.getDayOfWeek().getValue();
            for (Rule rule:list) {
                scheduling.setDeptId(rule.getDeptId());
                scheduling.setUserId(rule.getUserId());
                scheduling.setRuleId(rule.getId());
                Scheduling result1 = schedulingMapper.getScheduling(rule.getUserId(),"上午",date);
                Scheduling result2 = schedulingMapper.getScheduling(rule.getUserId(),"下午",date);
                if (rule.getWeek().substring(week*2-2,week*2-1).equals("1")){
                    if (result1 == null){
                        scheduling.setNoon("上午");
                        schedulingMapper.addScheduling(scheduling);
                    }
                } else {
                    if (result1 != null){
                        schedulingMapper.deleteScheduling(result1.getId());
                    }
                }
                if (rule.getWeek().substring(week*2-1,week*2).equals("1")){
                    if (result2 == null){
                        scheduling.setNoon("下午");
                        schedulingMapper.addScheduling(scheduling);
                    }
                } else {
                    if (result2 != null){
                        schedulingMapper.deleteScheduling(result2.getId());
                    }
                }

            }
            date = date.plusDays(1);
        }
        return true;
    }

    /**
     * 修改排班计划
     * @param list
     */
    @Override
    @Transactional
    public Boolean updateScheduling(List<Scheduling> list) {
        LocalDate date = LocalDate.now();
        for (Scheduling scheduling:list) {
            if (scheduling.getSchedDate().compareTo(date) >=0){
                Scheduling result = schedulingMapper.getScheduling(scheduling.getUserId(),scheduling.getNoon(),scheduling.getSchedDate());
                if (scheduling.getOnDuty()){
                    if (result == null){
                        schedulingMapper.addScheduling(scheduling);
                    }
                } else {
                    if(result != null){
                        schedulingMapper.deleteScheduling(result.getId());
                    }
                }
            }
        }
        return true;
    }
}
